#include "led.h"


// 初始化
void LED_Init(void)
{
    // 1. 时钟配置
	RCC->APB2ENR |= RCC_APB2ENR_IOPAEN;
	
	// 2. GPIO工作模式配置
	GPIOA->CRL &= ~GPIO_CRL_CNF1;
	GPIOA->CRL |= GPIO_CRL_MODE1;
  //GPIOA->ODR |= GPIO_ODR_ODR1;
	
	GPIOA->CRL &= ~GPIO_CRL_CNF2;
	GPIOA->CRL |= GPIO_CRL_MODE2;
  //GPIOA->ODR |= GPIO_ODR_ODR2;
	
	GPIOA->CRL &= ~GPIO_CRL_CNF3;
	GPIOA->CRL |= GPIO_CRL_MODE3;
  //GPIOA->ODR |= GPIO_ODR_ODR3;


	GPIOA->CRL &= ~GPIO_CRL_CNF4;
	GPIOA->CRL |= GPIO_CRL_MODE4;
  //GPIOA->ODR |= GPIO_ODR_ODR4;


  GPIOA->CRL &= ~GPIO_CRL_CNF5;
	GPIOA->CRL |= GPIO_CRL_MODE5;
  //GPIOA->ODR |= GPIO_ODR_ODR5;
	
	// 3. PA0、PA1、PA8输出高电平，默认关灯
    LED_Off(LED_R);
    LED_Off(LED_G);
    LED_Off(LED_B);
    LED_Off(LED_4);
    LED_Off(LED_5);
}

// 控制某个LED的开关
void LED_On(uint16_t led)
{
    GPIOA->ODR &= ~led;
}
void LED_Off(uint16_t led)
{
    GPIOA->ODR |= led;
}

// 翻转LED状态
void LED_Toggle(uint16_t led)
{
    // 需要先判断当前LED状态，读取IDR对应位
    if ((GPIOA->IDR & led) == 0)
    {
        LED_Off(led);
    }
    else
    {
        LED_On(led);
    }  
}

// 对一组LED灯，全开全关
void LED_OnAll(uint16_t leds[], uint8_t size)
{
    for (uint8_t i = 0; i < size; i++)
    {
        LED_On(leds[i]);
    }
}

void LED_OffAll(uint16_t leds[], uint8_t size)
{
    for (uint8_t i = 0; i < size; i++)
    {
        LED_Off(leds[i]);
    }
}

